package com.glory.wxelectricitypda.activity;


import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.FTDataAcqBinAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.FTDataAcqBinBean;
import com.glory.wxelectricitypda.bean.FTDataAcqNewBinBean;
import com.glory.wxelectricitypda.bean.PittedBean;
import com.glory.wxelectricitypda.bean.ResponseBean;

import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FTDataAcquisitionActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;
    @BindView(R.id.lotQty)
    TextView lotQty;

    @BindView(R.id.beginAcq)
    TextView beginAcq;

    @BindView(R.id.binMessageRecycler)
    RecyclerView binMessageRecycler;


    private FTDataAcqBinAdapter mFTDataAcqBinAdapter;
    private List<FTDataAcqBinBean.EDCSETLISTDTO> edcsetlist = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_data_acquisition;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("FT数据整合上报MES");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (lotID.getText().toString().equals("")) {
                    showToast("请扫描批次ID", false);
                    return;
                }
                submit();
            }
        });



        binMessageRecycler.setLayoutManager(new LinearLayoutManager(FTDataAcquisitionActivity.this));
        binMessageRecycler.setItemAnimator(new DefaultItemAnimator());
        binMessageRecycler.addItemDecoration(new DividerItemDecoration(FTDataAcquisitionActivity.this, 1));
        mFTDataAcqBinAdapter = new FTDataAcqBinAdapter(null, null);
        mFTDataAcqBinAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mFTDataAcqBinAdapter.isFirstOnly(true);
        binMessageRecycler.setAdapter(mFTDataAcqBinAdapter);

    }

    @Override
    protected void initData() {

        initEdit(lotID, s -> {
            queryFTDataAcq();
        });

    }

    private void queryFTDataAcq() {

        mFTDataAcqBinAdapter.setNewData(null);

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotID.getText().toString());
            jsonObject1.put("ACTIONTYPE", "GETLOTINFO");
            jsonObject.put("Data", jsonObject1);
            jsonObject.put("MessageName", "EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<PittedBean>>(
                new TypeToken<ResponseBean<PittedBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<PittedBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    lotQty.setText(String.valueOf(response.body().getBody().getLOT().getMAINQTY()));

                    queryBin();
                }
            }

            @Override
            public void onError(Response<ResponseBean<PittedBean>> response) {
                super.onError(response);
                loadError(response.getException(), "sendRequest");
            }
        });


//        JSONObject jsonObject = new JSONObject();
//        try {
//            jsonObject.put("EqpID", eqpID.getText().toString());
//            jsonObject.put("TesteqpId", testeqpId.getText().toString());
//            jsonObject.put("LotID", lotID.getText().toString());
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        NetUtils.requestPostUtils("getHbinQty","getHbinQty",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<FTDataAcqQueryBean>>(
//                new TypeToken<ResponseBean<FTDataAcqQueryBean>>() {
//                }.getType(), getSupportFragmentManager(), this) {
//            @Override
//            public void onSuccess(Response<ResponseBean<FTDataAcqQueryBean>> response) {
//                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
//
//                    lotQty.setText(response.body().getBody().getLotQty());
//                    testRecId.setText(response.body().getBody().getTestRecId());
//                    eqpState.setText(response.body().getBody().getEqpState());
//                    mFTDataAcqAdapter.setNewData(response.body().getBody().getTestProg());
//                    mFTDataAcqBinAdapter.setNewData(response.body().getBody().getHbinEDClist());
//                }
//            }
//
//            @Override
//            public void onError(Response<ResponseBean<FTDataAcqQueryBean>> response) {
//                super.onError(response);
//                loadError(response.getException(), "getHbinQty");
//            }
//        });
    }

    private void queryBin() {

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotID.getText().toString());
            jsonObject.put("Data", jsonObject1);
            jsonObject.put("MessageName", "EUIT.FTEDCCURRENT");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<FTDataAcqBinBean>>(
                new TypeToken<ResponseBean<FTDataAcqBinBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTDataAcqBinBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    edcsetlist.clear();
                    edcsetlist.addAll(response.body().getBody().getEDCSETLIST());
                    mFTDataAcqBinAdapter.setNewData(edcsetlist.get(0).getEDCSETBINLINELIST());
                    mFTDataAcqBinAdapter.setMainqty(lotQty.getText().toString());
                    mFTDataAcqBinAdapter.setType(false);

                }
            }

            @Override
            public void onError(Response<ResponseBean<FTDataAcqBinBean>> response) {
                super.onError(response);
                loadError(response.getException(), "sendRequest");
            }
        });


    }


    private void queryNewBin() {

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotID.getText().toString());
            jsonObject1.put("MAINQTY", lotQty.getText().toString());
//            jsonObject.put("Data", jsonObject1);
//            jsonObject.put("MessageName", "EUIT.FTEDCCURRENT");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("binresultReQuest", "binresultReQuest", jsonObject1, this, new FreeUI_DialogEntityCallBack<ResponseBean<FTDataAcqBinBean>>(
                new TypeToken<ResponseBean<FTDataAcqBinBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTDataAcqBinBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    mFTDataAcqBinAdapter.setNewData(null);
                    edcsetlist.clear();
                    edcsetlist.addAll(response.body().getBody().getEDCSETLIST());
                    mFTDataAcqBinAdapter.setNewData(edcsetlist.get(0).getEDCSETBINLINELIST());
                    mFTDataAcqBinAdapter.setType(true);
                    mFTDataAcqBinAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onError(Response<ResponseBean<FTDataAcqBinBean>> response) {
                super.onError(response);
                loadError(response.getException(), "binresultReQuest");
            }
        });


    }

    private void submit() {

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotID.getText().toString());
            jsonObject1.put("ISTEMP", false);
            jsonObject1.put("ISONLINE", false);
            jsonObject1.put("TOTALQTY", lotQty.getText().toString());
            List<FTDataAcqBinBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO> data = mFTDataAcqBinAdapter.getData();
//            edcsetlist.get(0).getEDCSETBINLINELIST().clear();
//            edcsetlist.get(0).getEDCSETBINLINELIST().addAll(data);
            JSONArray jsonArray = new JSONArray(new Gson().toJson(edcsetlist));
            jsonObject1.put("EDCSETLIST", jsonArray);
            jsonObject.put("Data", jsonObject1);
            jsonObject.put("MessageName", "EUIT.FTSAVEEDCDATA");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {

                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("提交成功", true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 2000);
                }


            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "sendRequest");
            }
        });

    }

    @OnClick({R.id.beginAcq})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.beginAcq:
//                mFTDataAcqBinAdapter.setNewData(null);
                queryNewBin();

                break;
        }
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }

}